﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Bingosoft.Security.Principal;
using Bingosoft.Common.Logging;
using HHMMS.FileServer;

namespace HHMMS.UI.Common
{
    /// <summary>
    /// 实现一个HTTP处理程序，用于在Web服务器上接收客户端上传的文件并转存到到文件服务器上
    /// 需在web服务器上的web.config中增加相应的配置。
    /// </summary>
    public class UploadHandler : IHttpHandler
    {
        private readonly ILog log = LogManager.GetCurrentClassLogger();
        private readonly IFileServerProvider fileServer = FileServerManager.Create();

        #region IHttpHandler Members

        public bool IsReusable
        {
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
            //获取POST过来的文件
            HttpFileCollection files = context.Request.Files;

            //判断是否有文件上传
            if (files == null || files.Count <= 0)
            {
                context.Response.Clear();
                context.Response.Write("文件服务器没有接收到任何文件！");
                context.Response.End();
                return;
            }

            //获取参数
            string attachTypeID, folder, mark1, mark2, mark3, mark4;

            //附件类型ID
            attachTypeID = context.Request["attachTypeID"];

            //保存目录
            folder = context.Request["folder"];

            //扩展业务标识
            mark1 = context.Request["mark1"];
            mark2 = context.Request["mark2"];
            mark3 = context.Request["mark3"];
            mark4 = context.Request["mark4"];

            //当前用户
            IUser user = Bingosoft.Security.SecurityContext.User;

            //将文件上传到文件服务器上
            try
            {
                //若指定了文件类型且未指定目录则初始化文件保存目录
                if (!string.IsNullOrEmpty(attachTypeID) && string.IsNullOrEmpty(folder))
                {
                    var attType = AttachmentTypeInfoProvider.Instance.SelectSingle(attachTypeID);
                    if (attType != null)
                    {
                        folder = attType.SaveDirName;
                    }
                }

                AttachmentInfo attach = fileServer.UploadToFileServer(files[0], folder, attachTypeID, user.Id, user.Name, mark1, mark2, mark3, mark4);
                var result = new
                {
                    succ = 1,
                    fileID = attach.ID,
                    fileName = attach.FileName,
                    fileUrl = attach.FileUrl,
                    errMsg = ""
                };
                context.Response.Write(result.ToJson());
            }
            catch (Exception ex)
            {
                log.Error("上传文件异常", ex);
                var result = new
                {
                    succ = 0,
                    fileName = "",
                    fileUrl = "",
                    errMsg = ex.Message
                };
                context.Response.Write(result.ToJson());
            }
        }

        #endregion
    }
}
